import os

from dao.evaluationdao import EvaluationDao
from dao.model_describedaao import ModelDescribeDao
from dao.traindatadao import TrainDataDao


class CleanById:
    edao = EvaluationDao()
    mdao = ModelDescribeDao()
    tdao = TrainDataDao()

    def clean(self, train_id):
        eacc_name = self.tdao.getEACCImgNameByTrainId(train_id)
        model_img_name = self.tdao.getModelImgNameByTrainId(train_id)
        name_tuple = self.edao.getImgNamesByTrainId(train_id)
        self.edao.deleteByTrainId(train_id)
        self.mdao.deleteByTrainId(train_id)
        self.tdao.deleteById(train_id)
        if os.path.exists(f'../storage/{train_id}.h5'):
            os.remove(f'../storage/{train_id}.h5')
        names = []
        for name_tuple1 in name_tuple:
            if name_tuple1 is not None:
                if name_tuple1[0] is not None:
                    names.append(name_tuple1[0])
                if name_tuple1[1] is not None:
                    names.append(name_tuple1[1])
        print(len(names))
        if eacc_name is not None:
            names.append(eacc_name)
        if model_img_name is not None:
            names.append(model_img_name)
        # print(names)
        for name in names:
            os.remove("../" + name)

if __name__ == '__main__':

    for i in range(200, 250):
        CleanById().clean(i)
